package leetcode.editor.cn;
//
// 
// 给你两棵二叉树 root 和 subRoot 。检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树。如果存在，返回 true ；否则
//，返回 false 。 
// 
// 
//
// 二叉树 tree 的一棵子树包括 tree 的某个节点和这个节点的所有后代节点。tree 也可以看做它自身的一棵子树。 
//
// 
//
// 示例 1： 
// 
// 
//输入：root = [3,4,5,1,2], subRoot = [4,1,2]
//输出：true
// 
//
// 示例 2： 
// 
// 
//输入：root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
//输出：false
// 
//
// 
//
// 提示： 
//
// 
// root 树上的节点数量范围是 [1, 2000] 
// subRoot 树上的节点数量范围是 [1, 1000] 
// -10⁴ <= root.val <= 10⁴ 
// -10⁴ <= subRoot.val <= 10⁴ 
// 
//
// Related Topics 树 深度优先搜索 二叉树 字符串匹配 哈希函数 👍 991 👎 0


//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution237 {
    boolean res = false;
    String sub;

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        StringBuilder subBuilder = new StringBuilder();
        StringBuilder rootBuilder = new StringBuilder();
        getPostOrder(subRoot, subBuilder);
        getPostOrder(root, rootBuilder);
        return rootBuilder.toString().contains(subBuilder.toString());
    }


    public boolean isSubtree1(TreeNode root, TreeNode subRoot) {
        StringBuilder builder = new StringBuilder();
        getPostOrder(subRoot, builder);
        sub=builder.toString();
        dfs(root);
        return res;
    }

    private void dfs(TreeNode node) {
        if (node == null) {
            return;
        }
        if (res) {
            return;
        }
        StringBuilder cur = new StringBuilder();
        getPostOrder(node, cur);
        if (sub.equals(cur.toString())) {
            res = true;
            return;
        }
        dfs(node.left);
        dfs(node.right);
    }

    private void getPostOrder(TreeNode node, StringBuilder postOder) {
        if (node == null) {
            postOder.append('#').append(',');
            return;
        }
        getPostOrder(node.left, postOder);
        getPostOrder(node.right, postOder);
        postOder.append(node.val).append(',');

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)
